package com.uchengwang.ui.cm_ui.uu.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayout.widget.MsgView;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.me.ui.MyGroupsActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.MobileContactActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.UsersActivity;
import com.uchengwang.ui.cm_ui.uu.bean.SlideBean;
import com.uchengwang.ui.cm_ui.uu.ui.AddFriendActivity;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/1/23.
 * 左侧滑动部分的adapter
 */

public class SlideLeftAdapter extends BaseAdapter{
    private Context context;
    private List<SlideBean> list = new ArrayList<>();
//    private int[] resIds = {R.drawable.icon_slide_atten, R.drawable.icon_slide_fans,
//            R.drawable.icon_slide_friends, R.drawable.icon_slide_group, R.drawable.icon_slide_contact};
//    private String[] titles = {"关注", "粉丝", "好友", "群组", "通讯录好友"};
    public SlideLeftAdapter(Context context, List<SlideBean> list){
        this.context = context;
        this.list = list;
    }

    public void setList(List<SlideBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<SlideBean> getList(){
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        SlideVH holder = null;
        if (view == null){
            holder = new SlideVH();
            view = LayoutInflater.from(context).inflate(R.layout.slide_item_layout_view, null);
            holder.slideLv_ln = (RelativeLayout) view.findViewById(R.id.slideLv_ln);
            holder.imageView = (ImageView) view.findViewById(R.id.slideLv_icon);
            holder.textView = (TextView) view.findViewById(R.id.slideLv_text);
            holder.slideLv_num = (TextView) view.findViewById(R.id.slideLv_num);
            holder.slideLv_notice = (MsgView) view.findViewById(R.id.slideLv_notice);

            view.setTag(holder);
            AutoUtils.autoSize(view);
        }else {
            holder = (SlideVH) view.getTag();
        }
        final SlideBean bean = list.get(position);

        ImageManager.getInstance().loadImage(context, holder.imageView, bean.getResId(), ImageManager.ImageType.NARMOL);
        holder.textView.setText(bean.getTitle());
        if (bean.getNum() >0){
            holder.slideLv_num.setText(bean.getNum()+"");
        }

        if (bean.getHveNoRead() >0){
            holder.slideLv_notice.setVisibility(View.VISIBLE);
            UnreadMsgUtils.show(holder.slideLv_notice, bean.getHveNoRead());
        }else {
            holder.slideLv_notice.setVisibility(View.GONE);
        }

        holder.slideLv_ln.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = null;
                switch (bean.getTitle()){
                    case "关注":
                        intent = new Intent(context, UsersActivity.class);
                        intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                        intent.putExtra(BaseConfig.TYPE, 2);
                        break;

                    case "粉丝":
                        intent = new Intent(context, UsersActivity.class);
                        intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                        intent.putExtra(BaseConfig.TYPE, 1);

                        if (bean.getHveNoRead() >0){
                            //重置我的新粉丝状态 设置为已获取
                            bean.setHveNoRead(0);
                            notifyDataSetChanged();
                            EventBus.getDefault().post(BaseConfig.EVENT_CANCEL);
                        }

                        break;

                    case "好友":
                        intent = new Intent(context, UsersActivity.class);
                        intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                        intent.putExtra(BaseConfig.TYPE, 3);
                        break;

                    case "群组":
                        intent = new Intent(context, MyGroupsActivity.class);
                        break;
                    case "查看周边朋友":
                        intent = new Intent(context, AddFriendActivity.class);
                        intent.putExtra(BaseConfig.USER_ROLE, BaseConfig.STUDENT_ROLE);
                        intent.putExtra(BaseConfig.TYPE, 2);
                        break;

                    case "查看关系朋友":
                        intent = new Intent(context, AddFriendActivity.class);
                        intent.putExtra(BaseConfig.USER_ROLE, BaseConfig.STUDENT_ROLE);
                        intent.putExtra(BaseConfig.TYPE, 4);
                        break;

                    case "添加手机联系人":
                        intent = new Intent(context, MobileContactActivity.class);
                        break;
                }
                if (intent != null){
                    context.startActivity(intent);
                }


            }
        });

        return view;
    }

    private class SlideVH {
        private RelativeLayout slideLv_ln;
        private ImageView imageView;
        private TextView textView, slideLv_num;
        private MsgView slideLv_notice;
    }
}
